package job.tracker.service

import job.tracker.domain.*;

class JobService {

	static transactional = true

	def saveJobSite(User user, JobSite jobSite) {
		jobSite.save(flush: true)

		JobSiteUser jobSiteUser = new JobSiteUser(
				user : user,
				jobSite : jobSite
				).save(flush: true)

		user.addToJobSiteUsers(jobSiteUser)
	}
	
	def syncronizeJobs(JobSite jobSite, List<Job> addToDB, List<Job> removeFromDB) {
		
		//jobSite.jobs.clear()
		//jobSite.jobs*.delete()
		addToDB.each {
			it.save()
			jobSite.addToJobs(it)
		}
		
		removeFromDB.each {
			jobSite.removeFromJobs(it)
			it.delete()
		}
		
		jobSite.save()
	}
}
